package com.erb.controller.cgs;

import com.erb.constants.AdminInformConstants;
import com.erb.constants.LoanConstants;
import com.erb.entity.*;
import com.erb.service.cgs.SmloadService;
import com.erb.service.hzg.LoanUserService;
import com.erb.service.zwj.IInformService;
import com.sun.deploy.net.URLEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 前台公用控制器
 */
@Controller
@RequestMapping("/loan")
public class LoanController {
	@Resource
	LoanUserService service;
	@Resource
	private IInformService informService;
	@Resource
	private SmloadService smloadService;

	@RequestMapping("/loanmian")
	public String loanmian(HttpSession session) {
		List<Loan> allloan = smloadService.allloan();
		session.setAttribute("allloan", allloan);
		return "loan/loanmian";
	}


	//信用贷
	@RequestMapping("/credit")
	public String login(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/creditloan";
	}

	@RequestMapping("/agreeapply")
	public String agreeapply() {
		return "loan/agreeloanapply";
	}

	@RequestMapping("/apply")
	public String apply(HttpSession session, Apply apply) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println(apply.toString());
		apply.setLoantype(LoanConstants.CREDIT);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		smloadService.AddSmloan(apply);

		System.out.println(apply);
		// 保存成功后，向后台发送通知
		Inform inform = new Inform();
		inform.setSendTime(System.currentTimeMillis());
		inform.setInformSenderId(user.getId());
		inform.setInformRecipientsId(AdminInformConstants.ADMIN_ID_LABEL);
		inform.setInformTitle("用户贷款申请");
		inform.setInformContent(String.format("贷款用户【%s】向公司申请了一条贷款记录，请审核查阅。", user.getName()));
		informService.saveInform(inform);

		return "redirect:/loan/preapply";
	}


	@RequestMapping("/loansubmit")
	public String loansubmit(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.1%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//无抵押公积金贷
	@RequestMapping("/back-loanapplu")
	public String list(Pager pager, Model model) {
		//分页
		pager.page(smloadService.selProcount());
		System.out.println("申请行数 +" + smloadService.selProcount());
		pager.data = smloadService.selApplyPage(pager);
		model.addAttribute("pager", pager);

		List<Apply> applyList = smloadService.applylist();
		model.addAttribute("applylist", pager.data);
		return "loan/loanApply";
	}

	//查询房子和车子
	@RequestMapping("/selmodalapply")
	public String selmodalapply(int id, String applyMType, Model session) {
		System.out.println("id:" + id + "类型：" + applyMType);
		Apply apply = smloadService.seluid(id);
		System.out.println(apply.getUid());
		int appuid = apply.getUid();
      /*  JSONArray jsonArray = new JSONArray();
        Map map = new HashMap();*/
		if (applyMType.equals("万能房抵贷")) {
			List<Houseimf> houseimfs = smloadService.Houselist(appuid);
			if (houseimfs != null) {
				session.addAttribute("houseimfs", houseimfs);
				return "loan/loanShowhouseApply";
			} else {
				return "loan/loanShowallApply";
			}
		} else if (applyMType.equals("保单贷")) {
			List<Insuranceimf> insuranceimfs = smloadService.insurancelist(appuid);
			if (insuranceimfs != null) {
				session.addAttribute("insuranceimfs", insuranceimfs);
				return "loan/loanShowinsuranApply";
			} else {
				return "loan/loanShowallApply";
			}

		} else if (applyMType.equals("汽车抵押贷")) {
			List<Carimf> carlist = smloadService.carlists(appuid);
			if (carlist != null) {
				System.out.println("是否有数据" + carlist.size());
				session.addAttribute("carlistts", carlist);
				return "loan/loanShowApply";
			} else {
				return "loan/loanShowallApply";
			}

		} else if (applyMType.equals("公积金贷")) {
			List<Publicimf> publicimfs = smloadService.publiclist(appuid);
			if (publicimfs != null) {
				session.addAttribute("publicimfs", publicimfs);
				return "loan/loanShowpublicApply";
			} else {
				return "loan/loanShowallApply";
			}

		} else if (applyMType.equals("车主信用贷")) {
			List<Carimf> carlist = smloadService.carlists(appuid);
			if (carlist != null) {
				System.out.println("是否有数据" + carlist.size());
				session.addAttribute("carlistts", carlist);
				return "loan/loanShowApply";
			} else {
				return "loan/loanShowallApply";
			}

		} else if (applyMType.equals("业主贷")) {
			List<Houseimf> houseimfs = smloadService.Houselist(appuid);
			if (houseimfs != null) {
				session.addAttribute("houseimfs", houseimfs);
				return "loan/loanShowhouseApply";
			} else {
				return "loan/loanShowallApply";
			}

		}
		return "loan/loanShowallApply";
	}

	@RequestMapping("/updapplyst")
	@ResponseBody
	public String updapply(String audids, String submitUrl) {
		System.out.println("审核为：" + submitUrl);
		System.out.println("id：" + audids);
		int bts = 3;
		if (submitUrl.equals("同意")) {
			bts = LoanConstants.SMLOANSTATE_AGREE;
			smloadService.updApply(bts, Integer.parseInt(audids));
		} else if (submitUrl.equals("拒绝")) {
			bts = LoanConstants.SMLOANSTATE_PASSAGREE;
			smloadService.updApply(bts, Integer.parseInt(audids));
		}
		return "通过";
	}

	@RequestMapping("/credit_1")
	public String credit_1(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_1loan";
	}


	@RequestMapping("/agreeapply1")
	public String agreeapply1() {
		return "loan/agreeloanapply1";
	}

	@RequestMapping("/apply1")
	public String apply1(HttpSession session, Apply apply, Publicimf publicimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_1);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		apply.setMaxmoney(0);
		apply.setMinmoney(0);
		smloadService.AddSmloan(apply);
		Publicimf selectpublic = smloadService.selectpublic(user.getId());
		if (selectpublic == null) {
			String proimg = smloadService.proimg(pfile);
			publicimf.setUid(user.getId());
			publicimf.setPublicprove(proimg);
			smloadService.addpubliceimf(publicimf);
		}
		return "redirect:credit_1";
	}


	@RequestMapping("/application")
	public String application1(HttpSession session, String loantype, int applyid) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Apply selectstate = smloadService.Selectstate(user.getId(), loantype, applyid);
		session.setAttribute("maxmoney", selectstate.getMaxmoney());
		session.setAttribute("minmoney", selectstate.getMinmoney());
		session.setAttribute("applyid", applyid);
		session.setAttribute("loantype", loantype);
		session.setAttribute("selectstate", selectstate);
		System.out.println(selectstate.getMaxmoney());
		System.out.println(selectstate.getMinmoney());
		if (loantype.equals(LoanConstants.CREDIT)) {
			return "/loan/creditloanapply";
		} else if (loantype.equals(LoanConstants.CREDIT_1)) {
			return "/loan/creditloanapply1";
		} else if (loantype.equals(LoanConstants.CREDIT_2)) {
			return "/loan/creditloanapply2";
		} else if (loantype.equals(LoanConstants.CREDIT_3)) {
			return "/loan/creditloanapply3";
		} else if (loantype.equals(LoanConstants.CREDIT_4)) {
			return "/loan/creditloanapply4";
		} else if (loantype.equals(LoanConstants.CREDIT_5)) {
			List<Map> diyacarimf = smloadService.diyacarimf(user.getId(), applyid, LoanConstants.CAROWNER_PUBLICE);
			System.out.println(diyacarimf.toString());
			if (diyacarimf.size() != 0) {
				session.setAttribute("diyacarimf", diyacarimf);
				session.setAttribute("diyanull", 1);
			} else {
				session.setAttribute("diyanull", 0);
			}

			return "/loan/creditloanapply5";
		} else if (loantype.equals(LoanConstants.CREDIT_6)) {
			List<Map> diyahouserimf = smloadService.diyahouserimf(user.getId(), applyid, LoanConstants.CAROWNER_PUBLICE);
			System.out.println(diyahouserimf.toString());
			if (diyahouserimf.size() != 0) {
				session.setAttribute("diyahouserimf", diyahouserimf);
				session.setAttribute("diyanull", 1);
			} else {
				session.setAttribute("diyanull", 0);
			}
			return "/loan/creditloanapply6";
		} else {
			return "redirect:preloan";
		}
	}


	@RequestMapping("/loansubmit1")
	public String loansubmit1(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_1, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.3%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT_1);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_1, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	@RequestMapping("/loandata")
	public String loandate1(HttpSession session, HttpServletRequest request, int lid, String loantype) {

		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan hetongloan = smloadService.hetongloan(lid, loantype);
		session.setAttribute("loan1", hetongloan);
		if (loantype.equals(LoanConstants.CREDIT)) {
			return "/loan/loandate";
		} else if (loantype.equals(LoanConstants.CREDIT_1)) {
			Publicimf selectpublic = smloadService.selectpublic(user.getId());
			session.setAttribute("public", selectpublic);
			return "/loan/loandate1";
		} else if (loantype.equals(LoanConstants.CREDIT_2)) {
			List<Map> selectcarimf = smloadService.selectcarimf(user.getId());
			if (selectcarimf.size() == 0) {
				session.setAttribute("carnull", 0);
			} else {
				session.setAttribute("carnull", 1);
				session.setAttribute("carimf", selectcarimf);
				session.setAttribute("selectsize", selectcarimf.size());
			}
			return "/loan/loandate2";
		} else if (loantype.equals(LoanConstants.CREDIT_3)) {
			List<Map> selectloandata = smloadService.selecthouseimf(user.getId());
			if (selectloandata.size() == 0) {
				session.setAttribute("housenull", 0);
			} else {
				session.setAttribute("housenull", 1);
				session.setAttribute("houseimf", selectloandata);
				session.setAttribute("selectsize", selectloandata.size());
			}
			return "/loan/loandate3";
		} else if (loantype.equals(LoanConstants.CREDIT_4)) {
			Insuranceimf selectinsuranceimf = smloadService.selectinsuranceimf(user.getId());
			session.setAttribute("insuranceimf", selectinsuranceimf);
			return "/loan/loandate4";
		} else if (loantype.equals(LoanConstants.CREDIT_5)) {
			List<Map> selectcarimf = smloadService.diyacarimf(user.getId(), lid, LoanConstants.CAROWNER_PUBLICE);
			System.out.println("7777777777777777777777777777777" + lid + "," + selectcarimf.size());
			if (selectcarimf.size() == 0) {
				session.setAttribute("carnull", 0);
			} else {
				session.setAttribute("carnull", 1);
				session.setAttribute("carimf", selectcarimf);
			}

			return "/loan/loandate5";
		} else if (loantype.equals(LoanConstants.CREDIT_6)) {
			List<Map> diyahouserimf = smloadService.diyahouserimf(user.getId(), lid, LoanConstants.CAROWNER_PUBLICE);
			if (diyahouserimf.size() == 0) {
				session.setAttribute("housenull", 0);
			} else {
				session.setAttribute("housenull", 1);
				session.setAttribute("diyahouserimf", diyahouserimf);
			}
			return "/loan/loandate6";
		} else {
			return "loan/loandata";
		}


	}


	//无抵押贷汽车贷
	@RequestMapping("/credit_2")
	public String credit_2(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_2loan";
	}


	@RequestMapping("/agreeapply2")
	public String agreeapply2(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectloandata = smloadService.selectcarimf(user.getId());
		if (selectloandata.size() == 0) {
			session.setAttribute("carnull", 0);
		} else {
			session.setAttribute("carnull", 1);
			session.setAttribute("carimf", selectloandata);
		}
		return "loan/agreeloanapply2";
	}

	@RequestMapping("/apply2")
	public String apply2(HttpSession session, Apply apply) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_2);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		smloadService.AddSmloan(apply);
		return "redirect:credit_2";
	}

	@RequestMapping("/addcar")
	public String addcar() {
		return "/loan/addcar";
	}


	@RequestMapping("/carisnull")
	@ResponseBody
	public Map carisnull(String caridcard) {
		List<Map> carisnull = smloadService.carisnull(caridcard);
		Map map = new HashMap();
		if (carisnull.size() > 0) {
			map.put("valid", false);
		} else {
			map.put("valid", true);
		}
		return map;
	}


	@RequestMapping("/caradd1")
	public String caradd1(HttpSession session, Carimf carimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		String proimg = smloadService.proimg(pfile);
		carimf.setUid(user.getId());
		carimf.setCarprove(proimg);
		carimf.setCarowner(LoanConstants.CAROWNER_USER);
		carimf.setCarapplyid(0);
		smloadService.addcarimf(carimf);
		int applyid = (int) session.getAttribute("applyid");
		return "redirect:carprove?applyid=" + applyid;
	}


	@RequestMapping("/loansubmit2")
	public String loansubmit2(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_2, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.7%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT_2);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_2, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//无抵押贷房贷
	@RequestMapping("/credit_3")
	public String credit_3(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_3loan";
	}


	@RequestMapping("/agreeapply3")
	public String agreeapply3(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectloandata = smloadService.selecthouseimf(user.getId());
		if (selectloandata.size() == 0) {
			session.setAttribute("housenull", 0);
		} else {
			session.setAttribute("housenull", 1);
			session.setAttribute("houseimf", selectloandata);
		}
		return "loan/agreeloanapply3";
	}

	@RequestMapping("/apply3")
	public String apply3(HttpSession session, Apply apply) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_3);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		smloadService.AddSmloan(apply);
		return "redirect:credit_3";
	}

	@RequestMapping("/addhouse")
	public String addhouse() {
		return "/loan/addhouse";
	}


	@RequestMapping("/houseisnull")
	@ResponseBody
	public Map houseisnull(String housedata) {
		List<Map> houseisnull = smloadService.houseisnull(housedata);
		System.out.println(houseisnull.size());
		Map map = new HashMap();
		if (houseisnull.size() > 0) {
			map.put("valid", false);
		} else {
			map.put("valid", true);
		}
		return map;
	}


	@RequestMapping("/houseadd1")
	public String houseadd1(HttpSession session, Houseimf houseimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		System.out.println(pfile);
		String proimg = smloadService.proimg(pfile);
		houseimf.setUid(user.getId());
		houseimf.setHouseprove(proimg);
		houseimf.setHouseapplyid(0);
		smloadService.addhouseimf(houseimf);
		int applyid = (int) session.getAttribute("applyid");
		return "redirect:houseprove?applyid=" + applyid;
	}


	@RequestMapping("/loansubmit3")
	public String loansubmit3(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_3, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.2%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT_3);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_3, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//无抵押保单贷
	@RequestMapping("/credit_4")
	public String credit_4(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_4loan";
	}


	@RequestMapping("/agreeapply4")
	public String agreeapply4() {
		return "loan/agreeloanapply4";
	}

	@RequestMapping("/apply4")
	public String apply4(HttpSession session, Apply apply, Insuranceimf insuranceimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_4);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		apply.setMaxmoney(0);
		apply.setMinmoney(0);
		smloadService.AddSmloan(apply);
		Insuranceimf insuranceimf1 = smloadService.selectinsuranceimf(user.getId());
		if (insuranceimf1 == null) {
			String proimg = smloadService.proimg(pfile);
			insuranceimf.setUid(user.getId());
			insuranceimf.setInsuranceprove(proimg);
			smloadService.addinsuranceimf(insuranceimf);
		}
		return "redirect:credit_4";
	}


	@RequestMapping("/loansubmit4")
	public String loansubmit4(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_4, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.1%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT_4);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_4, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//汽车抵押贷
	@RequestMapping("/credit_5")
	public String credit_5(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_5loan";
	}


	@RequestMapping("/addcar1")
	public String addcar1() {
		return "/loan/addcar1";
	}


	@RequestMapping("/caradd")
	public String caradd(HttpSession session, Carimf carimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		String proimg = smloadService.proimg(pfile);
		carimf.setUid(user.getId());
		carimf.setCarprove(proimg);
		carimf.setCarowner(LoanConstants.CAROWNER_USER);
		carimf.setCarapplyid(0);
		smloadService.addcarimf(carimf);
		return "redirect:agreeapply2";
	}

	@RequestMapping("/agreeapply5")
	public String agreeapply6(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectloandata = smloadService.selectcarimf(user.getId());
		if (selectloandata.size() == 0) {
			session.setAttribute("carnull", 0);
		} else {
			session.setAttribute("carnull", 1);
			session.setAttribute("carimf", selectloandata);
		}
		return "loan/agreeloanapply5";
	}

	@RequestMapping("/apply5")
	public String apply6(HttpSession session, Apply apply) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_5);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		smloadService.AddSmloan(apply);
		return "redirect:credit_5";
	}


	@RequestMapping("/carprove")
	public String carprove(HttpSession session, int applyid) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectcarimf = smloadService.selectcarimf(user.getId());
		session.setAttribute("carimf", selectcarimf);
		session.setAttribute("applyid", applyid);
		return "loan/carprove";
	}

	@RequestMapping("/prove")
	public String houseproveprove(HttpSession session, int carid, int applyid) {
		smloadService.updatecarimf_user(carid, applyid);
		return "redirect:carprove?applyid=" + applyid;
	}


	@RequestMapping("/loansubmit5")
	public String loansubmit5(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_5, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.0%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);

			loan.setLoantype(LoanConstants.CREDIT_5);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_5, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//万能抵押贷
	@RequestMapping("/credit_6")
	public String credit_6(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		LoanUser loanUser = service.lulist(user.getId());
		String idca = loanUser.getIdcard();
		if (idca == null || idca == "") {
			loanUser.setIdcard("no");
			session.setAttribute("forapplys", loanUser);
		} else {
			loanUser.setIdcard("yes");
			session.setAttribute("forapplys", loanUser);
		}
		return "loan/credit_6loan";
	}


	@RequestMapping("/addhouse1")
	public String addhouse1(HttpSession session, String loantype, int applyid) {
		session.setAttribute("loantype", loantype);
		session.setAttribute("applyid", applyid);
		return "/loan/addhouse1";
	}


	@RequestMapping("/houseadd")
	public String houseadd(HttpSession session, Houseimf houseimf, MultipartFile pfile) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		String proimg = smloadService.proimg(pfile);
		houseimf.setUid(user.getId());
		houseimf.setHouseprove(proimg);
		houseimf.setHouseowner(LoanConstants.HOUSEOWNER_USER);
		houseimf.setHouseapplyid(0);
		smloadService.addhouseimf(houseimf);
		return "redirect:agreeapply6";
	}

	@RequestMapping("/agreeapply6")
	public String agreeapply5(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selectloandata = smloadService.selecthouseimf(user.getId());
		if (selectloandata.size() == 0) {
			session.setAttribute("housenull", 0);
		} else {
			session.setAttribute("housenull", 1);
			session.setAttribute("houseimf", selectloandata);
		}
		return "loan/agreeloanapply6";
	}

	@RequestMapping("/apply6")
	public String apply5(HttpSession session, Apply apply) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		apply.setLoantype(LoanConstants.CREDIT_6);
		apply.setState(LoanConstants.SMLOANSTATE_NOTAGREE);
		apply.setUid(user.getId());
		smloadService.AddSmloan(apply);
		return "redirect:credit_6";
	}


	@RequestMapping("/houseprove")
	public String houseprove(HttpSession session, int applyid, String loantype) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		List<Map> selecthouseimfs = smloadService.selecthouseimf(user.getId());
		session.setAttribute("houseimf", selecthouseimfs);
		session.setAttribute("applyid", applyid);
		session.setAttribute("loantype", loantype);
		return "loan/houseprove";
	}

	@RequestMapping("/prove1")
	public String prove(HttpSession session, int houseid, int houseapplyid, String loantype) throws UnsupportedEncodingException {
		smloadService.updatehouseimf_user(houseid, houseapplyid);
		return "redirect:houseprove?applyid=" + houseapplyid + "&loantype=" + URLEncoder.encode(loantype, "UTF-8");
	}


	@RequestMapping("/loansubmit6")
	public String loansubmit6(HttpSession session, Loan loan) throws UnsupportedEncodingException {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		Loan loan1 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_6, loan.getApplyid());
		session.setAttribute("applyid", loan.getApplyid());
		if (loan1 == null) {
			loan.setUid(user.getId());
			loan.setLoaninterest("1.0%");
			loan.setRepayment(0);
			loan.setLoanstartdate(new Date());
			loan.setLoanpaydate(new Date());
			loan.setLoanstate(LoanConstants.SMLOANSTATE_NOTAGREE);
			loan.setLoantype(LoanConstants.CREDIT_6);
			smloadService.addloansubmit(loan);
		}
		Loan loan2 = smloadService.loanState(user.getId(), LoanConstants.CREDIT_6, loan.getApplyid());
		return "redirect:loandata?lid=" + loan2.getApplyid() + "&loantype=" + URLEncoder.encode(loan2.getLoantype(), "UTF-8");
	}


	//我的网贷

	@RequestMapping("/preapply")
	public String preaaply(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId(); //获取session的id待写
		String applyid = "";
		LoanUser loanUsers = service.lulist(id);
		session.setAttribute("loanUsers", loanUsers);

		List<Loan> selectloan = smloadService.selectloan(id);
		if (selectloan.size() != 0) {
			for (Loan loan : selectloan) {
				applyid = applyid + "," + loan.getApplyid();
			}
			applyid = applyid.substring(1);

		} else {
			applyid = "0";
		}
		List<Apply> selectapply = smloadService.selectapply(id, applyid);

		session.setAttribute("listapply", selectapply);
		if (selectapply.size() == 0) {
			session.setAttribute("applyis", 0);
		} else {
			session.setAttribute("applyis", 1);
		}

		return "/loan/alone/preapply";
	}

	@RequestMapping("/preloan")
	public String preloan(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId(); //获取session的id待写
		String applyid = "";
		LoanUser loanUsers = service.lulist(id);
		session.setAttribute("loanUsers", loanUsers);

		List<Loan> selectloan = smloadService.selectloan(id);
		session.setAttribute("listloan", selectloan);
		if (selectloan.size() == 0) {
			session.setAttribute("loanis", 0);
		} else {
			session.setAttribute("loanis", 1);
		}
		return "/loan/alone/preloan";
	}

	@RequestMapping("/prepayloan")
	public String prepayloan(HttpSession session) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId(); //获取session的id待写
		String applyid = "";
		LoanUser loanUsers = service.lulist(id);
		session.setAttribute("loanUsers", loanUsers);

		List<Loan> selectloan = smloadService.selectpayloan(id, LoanConstants.SMLOANSTATE_AGREE);
		session.setAttribute("listloan", selectloan);
		if (selectloan.size() == 0) {
			session.setAttribute("loanis", 0);
		} else {
			session.setAttribute("loanis", 1);
		}
		return "/loan/alone/prepayloan";
	}

	@RequestMapping("/payloan")
	public String payloan(HttpSession session, int lid) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId(); //获取session的id待写
		String applyid = "";
		LoanUser loanUsers = service.lulist(id);
		session.setAttribute("loanUsers", loanUsers);


		List<Payloan> nopaylist = smloadService.nopaylist(lid, LoanConstants.NOPAYLOAN);
		if (nopaylist.size() != 0) {
			Loan selectloan = smloadService.payloan(lid);
			List<Payloan> paylist = smloadService.paylist(lid);
			Payloan loaning = smloadService.loaning(lid);
			int pay = smloadService.paydate(lid);
			String loaninterest = selectloan.getLoaninterest();
			String loaninterest1 = loaninterest.substring(0, loaninterest.indexOf("%"));
			double v = Double.parseDouble(loaninterest1) / 100;
			double totlemoeny = selectloan.getMoney() + (selectloan.getMoney() * v);
			double repayment = selectloan.getRepayment() + (selectloan.getRepayment() * v);
			//离下期还款天数
			session.setAttribute("pay", pay);
			Payloan payloan = nopaylist.get(0);
			session.setAttribute("nopayloan", payloan);
			session.setAttribute("nopay", 1);
			session.setAttribute("loaning", loaning);
			session.setAttribute("totlemoeny", totlemoeny);
			session.setAttribute("repayment", repayment);
			session.setAttribute("payloan", selectloan);
			session.setAttribute("paylist", paylist);
			session.setAttribute("allpay", 1);
		} else {
			Loan selectloan = smloadService.payloan(lid);
			List<Payloan> paylist = smloadService.paylist(lid);
			String loaninterest = selectloan.getLoaninterest();
			String loaninterest1 = loaninterest.substring(0, loaninterest.indexOf("%"));
			double v = Double.parseDouble(loaninterest1) / 100;
			double totlemoeny = selectloan.getMoney() + (selectloan.getMoney() * v);
			double repayment = selectloan.getRepayment() + (selectloan.getRepayment() * v);
			session.setAttribute("totlemoeny", totlemoeny);
			session.setAttribute("repayment", repayment);
			session.setAttribute("payloan", selectloan);
			session.setAttribute("paylist", paylist);

			session.setAttribute("allpay", 0);
		}
		return "/loan/alone/payloan";
	}

	@RequestMapping("/huanpay")
	public String huanpay(HttpSession session, Payloan payloan, String paytype) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId(); //获取session的id待写
		String applyid = "";
		LoanUser loanUsers = service.lulist(id);
		session.setAttribute("loanUsers", loanUsers);
		double v2 = payloan.getPaymoney();
		if (paytype.equals("dangpay")) {
			payloan.setPaidmoney(payloan.getPaymoney());
			Loan selectloan = smloadService.payloan(payloan.getLid());
			String loaninterest = selectloan.getLoaninterest();
			String loaninterest1 = loaninterest.substring(0, loaninterest.indexOf("%"));
			double v = Double.parseDouble(loaninterest1) / 100;
			double v1 = payloan.getPaymoney() - (payloan.getPaymoney() * v);
			payloan.setPaymoney(0.0);
			payloan.setPayinterest(0.0);
			payloan.setPayselfmoney(0.0);
			payloan.setPaystate(LoanConstants.PAYLOAN);
			int updatepay = smloadService.updatepay(payloan);
			int updateloan = smloadService.updateloan(v1, payloan.getLid());
			smloadService.updateuser(id, v2);
		} else {
			payloan.setPaidmoney(payloan.getPaymoney());
			Loan selectloan = smloadService.payloan(payloan.getLid());
			String loaninterest = selectloan.getLoaninterest();
			String loaninterest1 = loaninterest.substring(0, loaninterest.indexOf("%"));
			double v = Double.parseDouble(loaninterest1) / 100;
			double v1 = payloan.getPaymoney() - (payloan.getPaymoney() * v);
			double v3 = selectloan.getMoney() - selectloan.getRepayment() + ((selectloan.getMoney() - selectloan.getRepayment()) * v);
			payloan.setPaymoney(0.0);
			payloan.setPayinterest(0.0);
			payloan.setPayselfmoney(0.0);
			payloan.setPaystate(LoanConstants.PAYLOAN);
			int updatepay = smloadService.updatepayall(payloan);
			int updateloan = smloadService.updateloan(selectloan.getMoney() - selectloan.getRepayment(), payloan.getLid());
			smloadService.updateuser(id, v3);
		}

		return "redirect:payloan?lid=" + payloan.getLid();
	}


	@RequestMapping("/pwdloan")
	@ResponseBody
	public int pwdloan(HttpSession session, String paypwd, String money) {
		LoanUser user = (LoanUser) session.getAttribute("loanuser");
		int id = user.getId();
		double money1 = Double.parseDouble(money);
		LoanUser lulist = service.lulist(id);
		String bankpwd = lulist.getBankpwd();
		int date = 0;
		if (bankpwd == null) {
			date = -1;
		} else {
			if (bankpwd.equals(paypwd)) {
				if (lulist.getAvailableLimit() < money1) {
					date = 2;
				} else {
					date = 1;
				}

			} else {
				date = 0;
			}
		}

		return date;
	}

}
